package org.myosw.solr;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;


/**
 * 关键字 过滤
 * @author 陈磊
 *
 */
public class FilterKeyword 
{
	
	
	 public static long getcount(String keyword)
	 {
		 

			try {
				//module这个文件会自动在下面几个地方被搜寻：当前目录 、用户主目录 、classpath
				PropertiesConfiguration conf = new PropertiesConfiguration("system.properties");
				String solrUrl = conf.getString("solrUrl");
				
				HttpSolrServer server = new HttpSolrServer(solrUrl );
				server.setMaxRetries(1); // defaults to 0.  > 1 not recommended.
				server.setConnectionTimeout(5000); // 5 seconds to establish TCP
				server.setParser(new XMLResponseParser()); // binary parser is used by default
				server.setSoTimeout(1000);  // socket read timeout
				server.setDefaultMaxConnectionsPerHost(100);
				server.setMaxTotalConnections(100);
				server.setFollowRedirects(false);  // defaults to false
				server.setAllowCompression(true);
				
				SolrQuery query = new SolrQuery();
				query.setQuery(keyword);
				QueryResponse  response = server.query(query);
				SolrDocumentList docs = response.getResults();
				long total =docs.getNumFound();//查出总数
				return total;
				
			} catch (ConfigurationException | SolrServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		return 0;
		
	}

	
}
